/**
 * 
 */
package com.insford.crawling.fifaonline3;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.insford.crawling.fifaonline3.dto.Player;
import com.insford.crawling.fifaonline3.dto.Player.Status;
import com.insford.crawling.fifaonline3.dto.Team;

/**
 * @author 조백규
 *
 */
public class PlayerInfoCrawling {

	public static String uri = "http://fifa3.gamemeca.com/playerData/index.html?index=playerInfo&pid=";
	
	private Log log = LogFactory.getLog(PlayerInfoCrawling.class);
	
	private String inputFile = "D:\\DaumCloud\\android\\피파온라인3\\TEAMID.txt";
	
	private String playerFile = "D:\\DaumCloud\\android\\피파온라인3\\PLAYERNATION.txt";
	
	private String[] playerIds = null;
	
	private PrintWriter playerpw = null;
	
	public static String seperate = "|";
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		PlayerInfoCrawling teamInfo = new PlayerInfoCrawling();
		teamInfo.init();
		teamInfo.run();
		teamInfo.close();
	}

	public void init() {
		BufferedReader br = null;
		
		try {
			br = new BufferedReader(new FileReader(inputFile));
			String str = null;
			ArrayList<String> list = new ArrayList<String>();
			while ((str = br.readLine()) != null) {
				list.add(str.trim());
			}
			playerIds = list.toArray(new String[list.size()]);
			
			playerpw = new PrintWriter(playerFile);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void run() {
		for (int i = 0; i < playerIds.length; i++) {
			String id = playerIds[i];
			
			Player player = this.req(id);
			// 팀ID, 포메이션, 선택가능선수
			playerpw.println(player.getPlayerid() + seperate + player.getNation());
			
			if (i % 50 == 0) {
				System.out.println(i + " 호출");
			}
		}
	}
	
	public void close() {
		playerpw.flush();
		playerpw.close();
		
		playerpw.flush();
		playerpw.close();
	}
	

	public Player req(String teamId) {
		
		Player player = new Player();
		
		try {
			
			String result = request(uri + teamId, null);
			result = result.replace("\n", "");
			result = result.replace("\r", "");
			result = result.replace("\t", "");
			
			
			//팀 기본정보
			//String pattern = "<div class=\"nation\"><div class=\"name\">(.{1,20})</div>";
			String pattern = "</td><td class=\"profile1\">(.{1,20})</td>";

			Pattern p = Pattern.compile(pattern);
			Matcher m = p.matcher(result);
			
			if (m.find()) {
				player.setNation(m.group(1));
			}
		} catch (Exception e) {
			log.error(null, e);
		}
		
		
		return player;
	}
	
	
	public static String request(String url, String result) {
		
		if (result != null) {
			return result;
		}
		
		try {
			Thread.sleep(50);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		HttpClient client = null;
		HttpGet get = null;
		
		try {
			client = new DefaultHttpClient();
			get = new HttpGet(url);
			HttpResponse response = client.execute(get);
			
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				result = EntityUtils.toString(response.getEntity());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return request(url, result);
	}
}

